package com.ruoyi.lottery.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.lottery.domain.LotTask;
import com.ruoyi.lottery.domain.LotTaskUser;
import com.ruoyi.lottery.mapper.LotTaskUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/** 抽奖任务的刷新 */
@Component
public class LotTaskEveryDayRef {

    private static final Logger log = LoggerFactory.getLogger(LotTaskEveryDayRef.class);

    @Resource
    private LotTaskUserMapper lotTaskUserMapper;

    /** 每天进行刷新 */
    @Scheduled(cron = "0 0 0 * * ?")
    public void execute() {
        log.info("[抽奖任务]: -----------------开始刷新 任务-用户 中间表数据-----------------");

        // 1: 查询需要刷新的数据 ===> number_limit 值为1的数据(每日刷新)
        LambdaQueryWrapper<LotTaskUser> wrapper = Wrappers.lambdaQuery(LotTaskUser.class).eq(LotTaskUser::getNumberLimit, LotTask.NUMBER_LIMIT_EVERY_DAY);

        // 2: 把这些数据进行删除
        lotTaskUserMapper.delete(wrapper);

        log.error("[抽奖任务]: -----------------任务-用户 中间表数据刷新结束-----------------");
    }
}
